﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DoAn_BumTruong_DesignPattern
{
    class CommentController : Abstract
    {
        public List<Comments> GetCommentByArticle(string artcleID)
        {
            int articleID = Convert.ToInt32(artcleID);
            using (var db = new DBContextEntity())
            {
                var query = from cm in db.Commentes
                            where cm.ArticleID.Equals(articleID)
                            select cm;
                return query.ToList();
            }
        }
        //Count comment by ArticleID
        public int CountCommentByArticleID(string articleID)
        {
            int arID = Convert.ToInt32(articleID);
            using (var db = new DBContextEntity())
            {
                var query = (from cm in db.Commentes
                             where cm.ArticleID.Equals(arID)
                             select cm).Count();
                return query;
            }
        }
        public List<Comments> SelectAll()
        {
            using (var db = new DBContextEntity())
            {
                var query = from cm in db.Commentes
                            select cm;
                return query.ToList();
            }
        }
        //paging
        public List<Comments> Paging(string page, out int howManyPage)
        {
            int pageSize = GlobalConfiguration.PageSize;
            howManyPage = (int)Math.Ceiling((double)SelectAll().Count / (double)pageSize);
            return SelectAll().Skip(pageSize * Convert.ToInt32(page) - pageSize).Take(pageSize).ToList();
        }
        public override bool Add(object o)
        {
            try
            {
                Comments _cm = o as Comments;
                using (var db = new DBContextEntity())
                {
                    Comments cm = new Comments();
                    cm.Client = _cm.Client;
                    cm.CreateDate = _cm.CreateDate;
                    cm.Context = _cm.Context;
                    cm.Avata = _cm.Avata;
                    cm.ShowHide = _cm.ShowHide;
                    cm.ArticleID = _cm.ArticleID;
                    db.Commentes.Add(cm);
                    if(db.SaveChanges()>0)
                    {
                        return true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return false;
        }

        public override bool Delete(object o)
        {
            throw new NotImplementedException();
        }

        public override bool Update(object o)
        {
            throw new NotImplementedException();
        }

        //Update Show/Hide
        public bool UpdateShowHide(int commentID, bool sh)
        {
            using (var db = new DBContextEntity())
            {
                var comment = db.Commentes.Find(commentID);
                if (comment != null)
                {
                    comment.CommentID = commentID;
                    comment.ShowHide = sh;
                    db.SaveChanges();
                }
                return true;
            }
        }
    }
}